home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / console / svgatext.9 / svgatext / SVGATextCalc < prev   
Encoding:
Text File  |  1996-06-14  |  7.0 KB  |  242 lines

  1. #!/usr/local/bin/perl
  2.  
  3. use Getopt::Long;
  4.  
  5. #----------------------------------------------------------------------------
  6.  
  7. $h_minfreq = 30000;            ## Mininum scan frequency
  8. $h_maxfreq = 35500;            ## Maximum scan frequency
  9. $maxdot    = 70000000;        ## Maximum dot clock frequency (Hz)
  10.  
  11. $h_freq_error  = 0.991;        ## Margin for hsync adjustments 
  12. $h_extra_space = 1.27;        ## Extra Horizontal space factor
  13. $v_extra_space = 0.93;        ## Extra Vertical space factor
  14. $h_sync1_guess = 80;        ## Horizontal sync1 guess
  15. $h_sync2_guess = 80;        ## Horizontal sync2 guess
  16.  
  17. #----------------------------------------------------------------------------
  18.  
  19. ### Check command line ###
  20.  
  21. $Cmd_font = "8x16";            ## Default Font specification
  22. $Cmd_vsf  = 60;                ## Default Vertical Frequency.
  23. $Cmd_help = 0;
  24. $Cmd_dotclock = 0;
  25. $Cmd_verbose = 0;
  26. $Cmd_trick9  = 0;
  27.  
  28. $ret = GetOptions("help",      \$Cmd_help,
  29.                   "font:s",    \$Cmd_font,
  30.                   "vsf:i",     \$Cmd_vsf,
  31.                   "dot:f",     \$Cmd_dotclock,
  32.                   "verbose",   \$Cmd_verbose,
  33.                   "trick9",    \$Cmd_trick9,
  34.                   "hmargin:f", \$h_extra_space,
  35.                   "vmargin:f", \$v_extra_space);
  36.  
  37. if ($ARGV[0] eq "" || $Cmd_help || $ret == 0 || $Cmd_vsf == 0)
  38. {
  39.     print STDERR "Usage: SVGATextCalc <mode> [--font=fontspec] [--trick9] [--verbose]\n";
  40.     print STDERR "                      [--vsf=refresh(Hz)] [--dot=dotclock(MHz)]\n";
  41.     print STDERR "                      [--hmargin=factor] [--vmargin=factor]\n";
  42.     exit 1;
  43. }
  44.  
  45. ($font_width, $font_height)    = split('x', $Cmd_font);
  46. ($h_text_lines, $v_text_lines) = split(/x/,$ARGV[0]);
  47. $v_freq                        = $Cmd_vsf;
  48.  
  49.  
  50. ### Grab allowed ranges from /etc/Textconfig ###
  51. ($h_minfreq, $h_maxfreq, $maxdot) = getlimits();
  52.  
  53. if ($Cmd_verbose) {
  54.     print "Horizontal Frequency: $h_minfreq, $h_maxfreq, Maximum dot=$maxdot\n";
  55. }
  56.  
  57. #-----------------------------------------------------------------------------
  58.  
  59. ### Check horizontal frequency ###
  60.  
  61. $v_lines = $font_height * $v_text_lines / $v_extra_space;
  62. $h_freq  = $v_lines * $v_freq;
  63.  
  64. ### I control the vertical... ###
  65.  
  66. $v_res   = round($font_height, $v_lines * $v_extra_space);   ## Vertical resolution
  67. $v_start = $v_res;                                             ## Vertical start
  68. $v_end   = $v_start + $font_height;                             ## Vertical end
  69.  
  70. ### ...And the Horizontal ###
  71.  
  72. $h_pixels   = round(8, $h_text_lines * $font_width * $h_extra_space);
  73. $h_res      = round(8, $h_text_lines * $font_width);
  74. $pixelclock = $h_pixels * $h_freq;
  75. $h_start    = round(8, $h_res + $h_sync1_guess);
  76. $h_end      = round(8, $h_start + $h_sync2_guess);
  77.  
  78. ##
  79. ##    We must make every effort so that the user must make as few 
  80. ##    adjustments as possible in the output. If the user didn't 
  81. ##    specify a fixed dotclock and our horizontal frequency is out
  82. ##    of scale, we then recalculate the dotclock so that the frequency
  83. ##    will be as close as possible to the offending bound (either
  84. ##    upper or lower).
  85. ##
  86. ##    The code below only recalculates the new clock and puts it
  87. ##    into the variable $Cmd_dotclock. 
  88. ##
  89.  
  90. if (! $Cmd_dotclock)
  91. {
  92.     ##
  93.     ##    We adjust the frequency to the maximum or minimum
  94.     ##    allowed, and then generate the new dotclock to
  95.     ##    keep things "balanced".
  96.     ##
  97.  
  98.     if ($h_freq < $h_minfreq || $h_freq > $h_maxfreq)
  99.     {
  100.         if ($h_freq < $h_minfreq) { $newclock = $h_minfreq * (1+(1-$h_freq_error)) * $h_pixels; }
  101.         else                       { $newclock = $h_maxfreq * $h_freq_error * $h_pixels; }
  102.     }
  103.  
  104.     $Cmd_dotclock = $newclock / 1000000;
  105. }
  106.  
  107. ##
  108. ##    If we have a fixed dot clock request (either by the means of the --dot
  109. ##    command line switch or set by the piece of code above), we must recalc
  110. ##    the parameters to keep the screen in sync. There's no guarantee that
  111. ##    the resolution will be the same (remember, the asked one may not be
  112. ##    possible at all), but the program will make its best to make it close.
  113. ##
  114.  
  115. if ($Cmd_dotclock)
  116. {
  117.     ### Calculate multiplying factor to the new dotclock ###
  118.  
  119.     $h_divfactor = ($Cmd_dotclock * 1000000) / $pixelclock;
  120.  
  121.     ## 
  122.     ##    First, we check if the new HSF (changed with the new dotclock) still
  123.     ##    fits our range. If so, then we have a deal, and keep the new dotclock without
  124.     ##  a change on the expected resolution. Otherwhise, we have to hack the
  125.     ##    horizontal parameters to make it fit. Of course, that will change the
  126.     ##    number of characters on the line, but that's the way it is...
  127.     ##
  128.     
  129.     $h_freq_new = $h_freq * $h_divfactor;
  130.  
  131.     if ($h_freq_new >= $h_minfreq && $h_freq_new <= $h_maxfreq)
  132.     {    
  133.         $h_freq = $h_freq_new;
  134.     }
  135.     else
  136.     {
  137.         $h_res    = round(8, $h_res    * $h_divfactor);
  138.         $h_start  = round(8, $h_start  * $h_divfactor);
  139.         $h_end    = round(8, $h_end    * $h_divfactor);
  140.         $h_pixels = round(8, $h_pixels * $h_divfactor);
  141.     }
  142.  
  143.     $pixelclock = $Cmd_dotclock * 1000000;
  144. }
  145.  
  146. ##
  147. ##    Check the 9 pixels spacing trick
  148. ##    This uses a feature on the VGA to add spacing between characters.
  149. ##    Everything must be calculated as if we were using 8 pixel fonts, though.
  150. ##
  151. ##    The reported dotclock may not be the same as set with the --dot command line.
  152. ##    This feature is best explained in SVGATextMode documentation.
  153. ##
  154.  
  155. if ($Cmd_trick9 && $font_width == 8)
  156. {
  157.     $pixelclock = $pixelclock * 9 / 8;
  158.     $font_width = 9;
  159. }
  160.  
  161. if ($Cmd_verbose)
  162. {
  163.     printf STDERR "Resolution: %dx%d chars, ", $h_text_lines, $v_text_lines;
  164.     printf STDERR "font: %dx%d pixels, ", $font_width, $font_height;
  165.     printf STDERR "VSF=%d Hz, HSF=%.2f KHz\n", $h_freq / $v_lines, $h_freq/1000;
  166. }
  167.  
  168. ### Sanity checks ###
  169.  
  170. if ($h_freq < $h_minfreq || $h_freq > $h_maxfreq)
  171. {
  172.     print STDERR "\n*** WARNING: Horizontal frequency is out of range ***\n\n";
  173. }
  174.  
  175. if ($pixelclock > $maxdot)
  176. {
  177.     print STDERR "\n*** WARNING: Dotclock is out of range ***\n\n";
  178. }
  179.  
  180. ### Print the results ###
  181.  
  182. printf "# Generated by SVGATextCalc (paganini\@ism.com.br), %dHz, %.2fKHz\n" ,$h_freq/$v_lines, $h_freq/1000; 
  183. printf "\"T%dx%dx%d\"          %02.2f  %4.4s %4.4s %4.4s %4.4s  %4.4s %4.4s %4.4s %4.4s  font %dx%d  # %dHz/%.2fKHz\n" ,
  184.     int($h_res / (($Cmd_trick9) ? 8 : $font_width)),
  185.     int($v_res / $font_height),
  186.     $font_width,
  187.     $pixelclock/1000000,
  188.     int($h_res), int($h_start), int($h_end), int($h_pixels),
  189.     int($v_res), int($v_start), int($v_end), int($v_lines),
  190.     $font_width, $font_height,
  191.     $h_freq/$v_lines, $h_freq/1000;
  192.  
  193. exit(0);
  194.  
  195. #----------------------------------------------------------------------------------------
  196.  
  197. sub getlimits
  198. {
  199.     my($lowfreq, $hifreq, $dacspeed);
  200.  
  201.     if (! open (CFG, "/etc/TextConfig"))
  202.     {
  203.         print "Error opening /etc/TextConfig. Is SVGATextMode installed?\n";
  204.         exit 1;
  205.     }
  206.  
  207.     while (<CFG>)
  208.     {
  209.         if (m/^HorizSync[ \t]*(.*)/o)
  210.         {
  211.             @freqs       = split(/[,-]/,$1);
  212.             $lowfreq     = $freqs[0] * 1000;
  213.             $hifreq      = $freqs[$#freqs] * 1000;
  214.         }
  215.         elsif (m/^DacSpeed[ \t]*([0-9]+)/o)
  216.         {
  217.             $dacspeed = $1 * 1000000;
  218.         }
  219.     }
  220.  
  221.     if ($hifreq == 0 || $lowfreq == 0)
  222.     {
  223.         print STDERR "I need the HorizSync line in /etc/TextConfig.\n";
  224.         exit 1;
  225.     }
  226.  
  227.     if ($dacspeed == 0 || $lowfreq == 0)
  228.     {
  229.         print STDERR "I need the DacSpeed line in /etc/TextConfig.\n";
  230.         exit 1;
  231.     }
  232.  
  233.     return ($lowfreq, $hifreq, $dacspeed);
  234. }
  235.  
  236. #----------------------------------------------------------------------------------------
  237. sub round
  238. {
  239.     my($factor, $val) = @_;
  240.     return int($val / $factor) * $factor;
  241. }
  242.